import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/sales/api/v1';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询CBU销售订单管理'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询CBU销售订单详情'
});
const newOrderDesc = formatMessage({
    id: 'api.message.newOrderDesc',
    defaultMessage: '新增CBU销售订单'
});
const exportDesc = formatMessage({
    id: 'api.message.exportDesc',
    defaultMessage: '导出'
});
const modifyDesc = formatMessage({
    id: 'api.message.modifyDesc',
    defaultMessage: '修改CBU销售订单'
});
const finalApproveDesc = formatMessage({
    id: 'api.message.finalApprove',
    defaultMessage: 'CBU销售订单终审'
});
const cancelDesc = formatMessage({
    id: 'api.message.approveCancel',
    defaultMessage: 'CBU销售订单审核取消'
});
const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: 'CBU销售订单作废'
});
const approveDesc = formatMessage({
    id: 'api.message.approve',
    defaultMessage: 'CBU销售订单审核'
});
const submitDesc = formatMessage({
    id: 'api.message.submitDesc',
    defaultMessage: '提交CBU销售订单'
});


const getDealersDesc = formatMessage({
    id: 'api.message.getDealersDesc',
    defaultMessage: '查询总代信息'
});
const getProductsDesc = formatMessage({
    id: 'api.message.getProductsDesc',
    defaultMessage: '查询产品信息'
});
const getSaleProformaInvoicesDesc = formatMessage({
    id: 'api.message.getSaleProformaInvoicesDesc',
    defaultMessage: '查询PI形式发票'
});
const getCarriersDesc = formatMessage({
    id: 'api.message.getCarriersDesc',
    defaultMessage: '查询整车货代信息'
});
const getShippingPortsDesc = formatMessage({
    id: 'api.message.getShippingPortsDesc',
    defaultMessage: '查询发货港口'
});
const getSalePlanOrdersDesc = formatMessage({
    id: 'api.message.getSalePlanOrdersDesc',
    defaultMessage: '滚动计划查询'
});
const getMarketDepartmentDesc = formatMessage({
    id: 'api.message.getMarketDepartmentDesc',
    defaultMessage: '查询大区'
});

//初始化
export const getInitData = () => request(`${baseUrl}/ui/CBUSaleOrder/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getListDesc));
// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/CBUSaleOrders?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/CBUSaleOrders/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
//新增
export const newOrder = data => request(`${baseUrl}/CBUSaleOrders`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newOrderDesc));
// 导出
export const exportData = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/CBUSaleOrders/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDesc))
        .then(res => res.data);
};
//修改
export const modifyOrder = (id, data) => request(`${baseUrl}/CBUSaleOrders/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDesc));
//强制修改
export const forceOrder = (id, data) => request(`${baseUrl}/CBUSaleOrders/${id}/force`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDesc));
//提交
export const submitData = (id, data) => request(`${baseUrl}/CBUSaleOrders/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDesc));
//取消
export const cancelData = (id, data) => request(`${baseUrl}/CBUSaleOrders/${id}/cancel`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, cancelDesc));
//审核通过
export const approveData = (id, data) => request(`${baseUrl}/CBUSaleOrders/${id}/pass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));
//审核不通过
export const rejectData = (id, data) => request(`${baseUrl}/CBUSaleOrders/${id}/reject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDesc));
//终审通过
export const finalApproveData = (id, data) => request(`${baseUrl}/CBUSaleOrders/${id}/finalJudgment/pass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, finalApproveDesc));
//终审不通过
export const finalRejectData = (id, data) => request(`${baseUrl}/CBUSaleOrders/${id}/finalJudgment/reject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, finalApproveDesc));
//作废
export const abandonData = (id, data) => request(`${baseUrl}/CBUSaleOrders/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));
// 查询最近一次CBU销售订单
export const getLastTime = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/CBUSaleOrders/lastTimeOfCBUsaleorder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};


// 快捷查询总代
export const getDealersQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forCBUSaleOrder/simple?${queryString}`, null, dto.DealerSimpleList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 查询总代
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forCBUSaleOrder?${queryString}`, null, dto.DealerList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealersDesc));
};
// 查询产品列表
export const getProducts = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/products/forCBUSaleOrder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getProductsDesc));
};
// 查询PI形式发票
export const getSaleProformaInvoices = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/saleProformaInvoices/forCBUSaleOrder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getSaleProformaInvoicesDesc));
};
// 查询整车货代信息
export const getCarriers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/carriers/forCBUSaleOrder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getCarriersDesc));
};
// 查询发货港口
export const getShippingPorts = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/shippingPorts/forCBUSaleOrder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getShippingPortsDesc));
};
// 查询发货港口
export const getSalePlanOrders = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/salePlanOrders/forCBUSaleOrder?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getSalePlanOrdersDesc));
};
// 查询大区
export const getMarketDepartment = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};

// 查询国家
const getAreasDesc = formatMessage({
    id: 'api.message.getAreasDesc',
    defaultMessage: '查询国家'
});
export const getAreas = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/query?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};

// 查询大区
export const getMarketDepartmentSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};
export const getAreasSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};
//导入模板
export const importTemp = () => request(`${baseUrl}/CBUSaleOrders/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);

// 查询PI形式发票
export const getSaleProformaInvoicesDetail = id => request(`${baseUrl}/saleProformaInvoices/${id}/forSaleOrder`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getSaleProformaInvoicesDesc));
